package com.ls.lserp.modules.setting.service;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.CollectionsUtils;
import com.ls.lserp.modules.common.entity.UploadFile;
import com.ls.lserp.modules.material.entity.InventoryRequest;
import com.ls.lserp.modules.setting.dao.OgnizationDao;
import com.ls.lserp.modules.setting.dao.ProcessDao;
import com.ls.lserp.modules.setting.entity.Ognization;
import com.ls.lserp.modules.setting.entity.Process;
import com.ls.lserp.modules.sys.utils.SequenceUtil;
import com.ls.lserp.modules.sys.utils.UserUtils;




@Service
@Transactional(readOnly = true)
public class ProcessService extends CrudService<ProcessDao, Process>{	
	@Autowired
    private ProcessDao processDao ;
	
	 @Autowired
	    private OgnizationDao ognizationDao;


	
	/**
     * @Title: findList
     * @Description: 查询字段类型列表
     * @param @param page
     * @param @param process
     * @param @return
     * @return Page<Process> 
     * @throws
     */
	public Page<Process> findList(Page<Process> page, Process process) {
		 // 设置分页参数
		process.setPage(page);
	       // 执行分页查询
	       page.setList(processDao.findList(process));
	       
	       return page;
	}

	/**
     * @Title: findShowProcess
     * @Description: 查询子项目
     * @param @param page
     * @param @param process
     * @param @return
     * @return List<Process> 
     * @throws
     */
   
    public List<Process> findListProcess(Process process) {
    	  // 执行查询
        return processDao.findListProcess(process);
   }

		
   
   /**
	 * ID查询一条数据
	 * 
	 * @param process
	 * @return
	 */
	public Process findACompany(Process process) {		
		return processDao.get(process);
	}
	
	/**
	 * ID查询一条数据
	 * 
	 * @param process
	 * @return
	 */
	@Transactional(readOnly = false)
	public int deleteProcess(Process process) {		
		return processDao.delete(process);
	}
	
	
	 /**
		 * 保存一条数据
		 * 
		 * @param process
	 * @param i 
		 * @return
		 */
	 @Transactional(readOnly = false)
	 public void saveProcess(Process process){
		 	if (process.getId()==null){
				if ("fashionStyleCreate".equals(process.getImageFlg())) {
					process.preInsert();
					processDao.insert(process);
					processDao.saveUploadFile(process.getUploadFileData());
					processDao.insertCraftFile(process.getId(),process.getUploadFileData().getFileId());
				}else {
					
				process.preInsert();
				process.setState("1");
		    	processDao.insertAllAdd(process);
		    	if (process.getNameImagePath()!=null&&!"".equals(process.getNameImagePath())) {
		    			UploadFile uf = new UploadFile();
		    			uf.setFileKbn(0);
		    			uf.setModuleKbn((long) 5);
		    			uf.setFileSource((long) 1);
		    			uf.setFtpFile("/process");
		    			uf.setCreateTime(new Date());
		    			uf.setCreateUser(UserUtils.getUser().getId().toString());
		    			String nameImagePath = process.getNameImagePath();
		    			 String pathTemp ="";
		    			 String nameTemp="";
		    	          if (!StringUtils.isEmpty(nameImagePath)) {
		    	            	String[] fileTemp = nameImagePath.split("/");
		    	            	int lenTemp = fileTemp.length-1;
		    	             	nameTemp=fileTemp[lenTemp];
		    	                for (int j = 1; j < lenTemp; j++) {
		    	                	pathTemp += fileTemp[j] + "/";
		    	                }
		    	            }
		    	         uf.setUploadFile(nameTemp);
		    	         uf.setFilePath("/" + pathTemp);
		    			processDao.saveUploadFile(uf);
		    			processDao.insertCraftFile(process.getId(),uf.getFileId());
					}
				}
			// 编辑
			}else{
				// 更新数据
				process.preUpdate();
				processDao.update(process);
				Long craftId = processDao.getFileID(process.getId());
				if (craftId!=null) {
					UploadFile uf = new UploadFile();
					String nameImagePath = process.getNameImagePath();
					String pathTemp ="";
					String nameTemp="";
					if (process.getNameImagePath()!=null&&!"".equals(process.getNameImagePath())) {
						if (!StringUtils.isEmpty(nameImagePath)) {
							String[] fileTemp = nameImagePath.split("/");
							int lenTemp = fileTemp.length-1;
							nameTemp=fileTemp[lenTemp];
							for (int j = 1; j < lenTemp; j++) {
								pathTemp += fileTemp[j] + "/";
							}
						}
						uf.setUploadFile(nameTemp);
						uf.setFilePath("/" + pathTemp);
					}else {
						uf.setUploadFile("");
						uf.setFilePath("");
					}
					uf.setId(craftId);
					uf.preUpdate();
					processDao.updateFile(uf);
				}else if (craftId==null&&process.getNameImagePath()!=null&&!"".equals(process.getNameImagePath())) {
					UploadFile uf = new UploadFile();
	    			uf.setFileKbn(0);
	    			uf.setModuleKbn((long) 5);
	    			uf.setFileSource((long) 1);
	    			uf.setFtpFile("/process");
	    			uf.setCreateTime(new Date());
	    			uf.setCreateUser(UserUtils.getUser().getId().toString());
	    			String nameImagePath = process.getNameImagePath();
	    			 String pathTemp ="";
	    			 String nameTemp="";
	    	          if (!StringUtils.isEmpty(nameImagePath)) {
	    	            	String[] fileTemp = nameImagePath.split("/");
	    	            	int lenTemp = fileTemp.length-1;
	    	             	nameTemp=fileTemp[lenTemp];
	    	                for (int j = 1; j < lenTemp; j++) {
	    	                	pathTemp += fileTemp[j] + "/";
	    	                }
	    	            }
	    	         uf.setUploadFile(nameTemp);
	    	         uf.setFilePath("/" + pathTemp);
	    			processDao.saveUploadFile(uf);
	    			processDao.insertCraftFile(process.getId(),uf.getFileId());
				}
		}
	    	
	}
	 
	 private Object $(String string) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	     * @Title: selVenderList
	     * @Description: 查询
	     * @param @param ognization
	     * @param @return
	     * @return Page<ognization> 
	     * @throws
	     */
	   public List<Ognization> selVenderList(Ognization ognization) {
	       
	       return ognizationDao.findList(ognization);
	   }
	   
	   /**
	    * @Title: findBrand
	    * @Description: 查询
	    * @param @param venderId
	    * @param @return
	    * @return List<InventoryRequest>
	    * @throws
	    */
	  public List<Process> findBrand(String venderId) {
		  Process paramIr = new Process();
		  paramIr.setVenderId(Long.parseLong(venderId));
	      return processDao.findBrandList(paramIr);
	  }
	 
	 
	 
	 
	 
	 
}

	


